package kinectData.simulator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Enables sliding through graphs of accelerometer data.
 * 
 * @author Markus
 */
public class SimulatorGUI {

	private JFrame jf;
	private SimulatorThread st;
	private JButton startStopButton;
	private JSlider slider;
	private String file;
	private boolean ignoreIt = false;
	
	public SimulatorGUI(String file){
		this.file = file;
		st = new SimulatorThread(file, this);
		jf = new JFrame("Simulator");
		jf.setSize(1000,80);
		jf.setLocation(270, 650);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLayout(null);
		jf.setResizable(false);
		slider = new JSlider(0, 100, 0); 
		slider.setPaintTicks(true); 
		slider.setMajorTickSpacing(20); 
		slider.setMinorTickSpacing(1); 
		slider.addChangeListener(new ChangeListener() { 
		  public void stateChanged(ChangeEvent e ) {
			 if(!ignoreIt) st.gotoPosition(((JSlider) e.getSource()).getValue());
			 else ignoreIt = false;
		  } 
		} ); 
		slider.setSize(800,50);
		slider.setLocation(0, 0);
		jf.add(slider);
		startStopButton = new JButton("Start");
		startStopButton.setSize(180, 50);
		startStopButton.setLocation(810,5);
		startStopButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				changePlayMode();
			}
		});		
		jf.add(startStopButton);
		jf.setVisible(true);
	}
	
	private void changePlayMode(){
		st.start();
		startStopButton.setEnabled(false);
	}
	
	public void enableButton(){
		st = new SimulatorThread(file, this);
		startStopButton.setEnabled(true);
	}
	
	public void updateSlider(int pos){
		ignoreIt = true;
		slider.setValue(pos);
		jf.repaint();
	}
}
